{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 任务3 - 建模（2天）\n",
    "用逻辑回归、svm和决策树；随机森林和XGBoost进行模型构建，评分方式任意，如准确率等。（不需要考虑模型调参）  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import warnings\n",
    "from sklearn.preprocessing import scale\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from xgboost.sklearn import XGBClassifier\n",
    "import lightgbm as lgb\n",
    "\n",
    "\n",
    "# 读取数据集\n",
    "data_all = pd.read_csv('/home/infisa/wjht/project/DataWhale/data_all.csv', encoding='gbk')\n",
    "\n",
    "# 划分为5折交叉验证数据集\n",
    "df_y=data_all['status']\n",
    "df_X=data_all.drop(columns=['status'])\n",
    "df_X=scale(df_X,axis=0)  #将数据转化为标准数据\n",
    "#构建模型\n",
    "\n",
    "lr = LogisticRegression(random_state=2018,tol=1e-6)  # 逻辑回归模型\n",
    "\n",
    "tree = DecisionTreeClassifier(random_state=2018) #决策树模型\n",
    "\n",
    "svm = SVC(probability=True,random_state=2018,tol=1e-6)  # SVM模型\n",
    "\n",
    "forest=RandomForestClassifier(n_estimators=100,random_state=2018) #　随机森林\n",
    "\n",
    "Gbdt=GradientBoostingClassifier(random_state=2018) #CBDT\n",
    "\n",
    "Xgbc=XGBClassifier(random_state=2018)  #Xgbc\n",
    "\n",
    "gbm=lgb.LGBMClassifier(random_state=2018)  #lgb\n",
    "\n",
    "\n",
    "\n",
    "def muti_score(model):\n",
    "    warnings.filterwarnings('ignore')\n",
    "    accuracy = cross_val_score(model, df_X, df_y, scoring='accuracy', cv=5)\n",
    "    precision = cross_val_score(model, df_X, df_y, scoring='precision', cv=5)\n",
    "    recall = cross_val_score(model, df_X, df_y, scoring='recall', cv=5)\n",
    "    f1_score = cross_val_score(model, df_X, df_y, scoring='f1', cv=5)\n",
    "    auc = cross_val_score(model, df_X, df_y, scoring='roc_auc', cv=5)\n",
    "    print(\"准确率:\",accuracy.mean())\n",
    "    print(\"精确率:\",precision.mean())\n",
    "    print(\"召回率:\",recall.mean())\n",
    "    print(\"F1_score:\",f1_score.mean())\n",
    "    print(\"AUC:\",auc.mean())\n",
    "\n",
    "\n",
    "\n",
    "model_name=[\"lr\",\"tree\",\"svm\",\"forest\",\"Gbdt\",\"Xgbc\",\"gbm\"]\n",
    "for name in model_name:\n",
    "    model=eval(name)\n",
    "    print(name)\n",
    "    muti_score(model)\n",
    "\n",
    "\n",
    "'''\n",
    "lr\n",
    "准确率: 0.7890191148682617\n",
    "精确率: 0.6542724662896913\n",
    "召回率: 0.3377975457965613\n",
    "F1_score: 0.44525012166067884\n",
    "AUC: 0.7840451024530857\n",
    "tree\n",
    "准确率: 0.6962524533638791\n",
    "精确率: 0.39920670173446693\n",
    "召回率: 0.4157413593052284\n",
    "F1_score: 0.40705496051057793\n",
    "AUC: 0.6029856787858856\n",
    "svm\n",
    "准确率: 0.787758390223099\n",
    "精确率: 0.7351623295760905\n",
    "召回率: 0.24060335431243626\n",
    "F1_score: 0.36179547264664874\n",
    "AUC: 0.7640376541388867\n",
    "forest\n",
    "准确率: 0.7921756804332226\n",
    "精确率: 0.7135700690071172\n",
    "召回率: 0.2867128441334693\n",
    "F1_score: 0.40835414886475174\n",
    "AUC: 0.7752164698827589\n",
    "Gbdt\n",
    "准确率: 0.7938590063951863\n",
    "精确率: 0.6604108594441386\n",
    "召回率: 0.36633732991104395\n",
    "F1_score: 0.4708811551285791\n",
    "AUC: 0.7888240065764295\n",
    "Xgbc\n",
    "准确率: 0.7982740847293591\n",
    "精确率: 0.6829783239831001\n",
    "召回率: 0.3663162336064133\n",
    "F1_score: 0.47673826685376613\n",
    "AUC: 0.7914190511145234\n",
    "gbm\n",
    "准确率: 0.79049080811139\n",
    "精确率: 0.6421783397519263\n",
    "召回率: 0.3730354066312717\n",
    "F1_score: 0.47150438344663004\n",
    "AUC: 0.7776116341798183\n",
    "'''"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:tensorflow-gpu]",
   "language": "python",
   "name": "conda-env-tensorflow-gpu-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
